package ru.minidoc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import ru.minidoc.model.User;

public class UserDao extends AbstractDao {
    public User login(User user) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        User result = jdbcTemplate.query (
                "select id, login, password, fullname, manager from user where login = ? and password = ?",
                new Object[] { user.getLogin(), user.getPassword() }, new ResultSetExtractor<User>() {

                    @Override
                    public User extractData(ResultSet rs) throws SQLException, DataAccessException {
                        if (rs.next()) {
                            User user = new User();
                            user.setId(rs.getInt(1));
                            user.setLogin(rs.getString(2));
                            user.setPassword(rs.getString(3));
                            user.setFullname(rs.getString(4));
                            user.setManager(rs.getBoolean(5));
                            return user;
                        }
                        return null;
                    }
                });
        return result;
    }

    public int create(User user) {
        return queryAndReturnIdentity(
                "insert into user(login, password, fullname, manager) values(?,?,?,?);",
                new Object[] { user.getLogin(), user.getPassword(), user.getFullname(), user.isManager() });
    }

    public void delete(User user) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update(
                "delete from user where id = ?",
                new Object[] { user.getId() });
    }

    public List<User> getUserList() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<User> result = jdbcTemplate.query(
                "select id, login, password, fullname, manager from user",
                new RowMapper<User>() {

                    @Override
                    public User mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        User user = new User();
                        user.setId(rs.getInt(1));
                        user.setLogin(rs.getString(2));
                        user.setPassword(rs.getString(3));
                        user.setFullname(rs.getString(4));
                        user.setManager(rs.getBoolean(5));
                        return user;
                    }
                });
        return result;
    }

    public void update(User user) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update(
                "update user set login = ?, fullname=?, password=?, manager=? where id = ?",
                new Object[] {user.getLogin(), user.getFullname(), user.getPassword(), user.isManager(), user.getId() });
    }
}
